<?php
/*
 * Created on Jan 16, 2013
 * 
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once (dirname(__FILE__) . DIRECTORY_SEPARATOR . 'BpoDomain.php');

class YahooAds
{
	private $urlPrefix = "http://xml.yellowmoxie.overture.com/d/search/p/yellowmoxie/multi/xml/?Partner=yellowmoxie_sem1_us_search_test&mkt=us&maxCount=40&affilData=ip%3D209.203.91.74%26ua%3DMozilla%2F5.0+%28Windows%3B+U%3B+Windows+NT+6.1%3B+en-US%29+AppleWebKit%2F533.4+%28KHTML%2C+like+Gecko%29+Chrome%2F5.0.375.99+Safari%2F533.4%26xfip%3D99.40.31.65&serveUrl=http%3A%2F%2Fwww.yellowmoxie.com%2F%2Fresults.php&type=type_test&Keywords=";
	private $urlSuffix = "&termFilters=1-9ktfd&urlFilters=1-9ktfd&accountFilters=1-9ktfd&on=10&in=0";
	
	public function __construct()
	{
	}
	
	public function getKeywordAdCount($keyword)
	{
		$numResults = 0;
		
		$url 		= $this->urlPrefix . urlencode($keyword) . $this->urlSuffix;
		
		$content 	= file_get_contents($url);
		$xml 		= new SimpleXMLElement($content);
		
		//echo $content;
		//echo "\n";		
		
		$rsNodes = $xml->xpath('/Results/ResultSet');
		
		foreach ($rsNodes as $rsNode) {
			$numResults = $rsNode['numResults'];
//			
//			echo "numResults: " . $numResults;
//			echo "\n";
		}
		
		return $numResults;
	}
	
	public function getApiKeywordsAdCount($keywords)
	{
		$numResults = 0;
		
		$urls		= array();
		$arrReturn 	= array();
		
		foreach($keywords as $keyword) {	
			$urls[$keyword] = $this->urlPrefix . urlencode($keyword) . $this->urlSuffix;
		}
		
		$mh = curl_multi_init();		
		
		foreach($urls as $i => $url) {		
			$conn[$i] = curl_init($url);      
			curl_setopt($conn[$i], CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");      
			curl_setopt($conn[$i], CURLOPT_HEADER ,0);      
			curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,60);      
			curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,true);  // 设置不将爬取代码写到浏览器，而是转化为字符串     
			
			curl_multi_add_handle ($mh, $conn[$i]);
		}
		
		do{      
			curl_multi_exec($mh, $active);    
		} while($active);
		   
		foreach($urls as $i => $url) {      
			$content 	= curl_multi_getcontent($conn[$i]); // get xml content			
			$xml 		= new SimpleXMLElement($content);
		
			//echo $content;
			//echo "\n";		
			
			$rsNodes = $xml->xpath('/Results/ResultSet');
			
			foreach ($rsNodes as $rsNode) {
				$numResults = $rsNode['numResults'];
				
				$arrReturn[$i] = $numResults;
				
//				echo "numResults: " . $numResults;
//				echo "<br>";				
			}
		}
		   
		foreach($urls as $i=> $url) {      
			curl_multi_remove_handle($mh,$conn[$i]);      
			curl_close($conn[$i]);    
		}
		      
		curl_multi_close($mh);    
		
		return $arrReturn;
	}
	
	public function getKeywordsAdCount($conn, $keywords, $limitTime)
	{
		$returnKeywordAds = array();
		
		foreach (array_chunk($keywords, 1000) as $chunkKws) {	
			$this->getKeywordCache($conn, &$returnKeywordAds, $chunkKws, $limitTime);
			
			if (DEBUG) echo ".";
			if (DEBUG) echo "yahoo ad total: " . count($returnKeywordAds) . ',' .date("Y-m-d H:i:s")."\n";
		}
		
		return $returnKeywordAds;
	}
	
	private function getKeywordCache($conn, $returnKeywordAds, $chunkKws, $limitTime)
	{
		$strKw = '';
		foreach ($chunkKws as $kwd) {
			if (!empty($strKw)) 
				$strKw .= ',';
				
			$strKw .= "'" . addslashes($kwd) . "'";
		}
		
		$query 	= "SELECT keyword kw, ad_count adc FROM autobidding.yahoo_ads WHERE keyword IN ($strKw) AND modifiedTime > (UTC_TIMESTAMP() - $limitTime)";
		$result = mysql_query($query, $conn) or die("Invalid query: ".mysql_error($conn)."\nWhole query: ".$query);
		while ($row = mysql_fetch_assoc($result)) {
			$kwad = new YahooKeywordAd();
			
			$kwad->adCount =  $row['adc'];
			
			$returnKeywordAds[$row['kw']] = $kwad;
			
		}
		
		if (DEBUG && isset($api_cpc)) echo "cache yahoo ad count: " . count($returnKeywordAds) . ',' .date("Y-m-d H:i:s")."\n";
		
		$arrRequestAPIkeyword = array();
		foreach ($chunkKws as $kwd) {
			if(!array_key_exists($kwd, $returnKeywordAds)){
				$arrRequestAPIkeyword[] = $kwd;
			}
		}
		
		
		if (DEBUG) echo "request yahoo ad count: " . count($arrRequestAPIkeyword) . ',' .date("Y-m-d H:i:s")."\n";
		
			
		foreach($arrRequestAPIkeyword as $kw){
			$numResults = $this->getKeywordAdCount($kw);
			
			$kwad = new YahooKeywordAd();
			
			$kwad->adCount =  $numResults;
			
			$returnKeywordAds[$kw] = $kwad;
			
			$query = "INSERT INTO autobidding.yahoo_ads SET modifiedTime = UTC_TIMESTAMP(), keyword = '" . addslashes($kw) . "', ad_count = $numResults" .
				" ON DUPLICATE KEY UPDATE ad_count = VALUES(ad_count), modifiedTime = VALUES(modifiedTime)";
			$result = mysql_query($query, $conn) or die("Invalid query: ".mysql_error($conn)."\nWhole query: ".$query);
			
		}	
	}
}


?>
